package com.leyou.order.controller;

import com.leyou.order.dto.CartToOrderDTO;
import com.leyou.order.dto.OrderDTO;
import com.leyou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("/order")
    public ResponseEntity<Long> saveOrder(@RequestBody CartToOrderDTO cartToOrderDTO){
        Long orderId =  orderService.saveOrder(cartToOrderDTO);
        return ResponseEntity.ok(orderId);
    }

    @GetMapping("/order/{id}")
    public ResponseEntity<OrderDTO> findOrderById(@PathVariable Long id){
        OrderDTO orderDTO = orderService.findOrderById(id);
        return ResponseEntity.ok(orderDTO);
    }

    @GetMapping("/order/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable(value = "id") Long id){
       String url = orderService.getPayUrl(id);
       return ResponseEntity.ok(url);
    }

    @GetMapping("/order/state/{id}")
    public ResponseEntity<Integer> getPayState(@PathVariable(value = "id") Long id){
        Integer state = orderService.getPayState(id);
        return ResponseEntity.ok(state);
    }


}
